package com.c.ctgproject.ui.doorkeeper.material

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.c.ctgproject.base.BaseViewModel
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.RequestBody

class DoorkeeperMaterialViewModel : BaseViewModel() {

    private val mConfirmResult = MutableLiveData<String>()
    val confirmResult: LiveData<String> get() = mConfirmResult

    private val mListResult = MutableLiveData<String>()
    val listResult: LiveData<String> get() = mListResult


    private val mAllSeller = MutableLiveData<String>()
    val allSeller: LiveData<String> get() = mAllSeller

    fun getAllSeller() {
        MyLog.d("获取销售人员")
        launch({
            mAllSeller.value = ApiMethods.getApiService().getAllSeller(userToken).awaitSync()
        }, {
            mError.value = it.message.handlerNull()
        })
    }

    //获取列表信息
    fun getMaterialList(pageNum: Int, PageSize: Int, searchText: String = "", sellerId: Int = 0) {
        val map: MutableMap<String, Any> =
            java.util.HashMap()
        map["pageNum"] = pageNum
        map["PageSize"] = PageSize
        if (sellerId != 0) {
            map["createBy"] = sellerId
        }
        if (searchText != "") {
            map["fuzzyQuery"] = searchText
        }
        val obj = Gson().toJson(map)
        val body = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            obj
        )
        launch {
            mListResult.value = ApiMethods.getApiService().doorkeeperMaterialOutList(userToken, body).awaitSync()
        }
    }

    //出场确认
    fun outConfirm(truckTrafficId: Int, materialSalesOrderId: Int) {
        launch {
            mConfirmResult.value = ApiMethods.getApiService().doorkeeperMaterialOutConfirm(userToken, materialSalesOrderId, truckTrafficId).awaitSync()
        }
    }

}